package com.navidove.commons.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

@Service
public class I18NUtil {
	@Autowired
	private MessageSource messageSource;
	
	private static I18NUtil i18nUtil;
	
	public I18NUtil() {
	    i18nUtil = this;
	}
	
	private String _getMsg(String code, Object... args) {
	    return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
	}
	
	public static String getMsg(String key, Object... args) {
	    try {
	        return i18nUtil._getMsg(key, args);
	    } catch (NoSuchMessageException e) {
	        return key;
	    }
	}
}
